package com.sure.classsystemserver.security.home.Impl;

import com.sure.classsystemserver.common.CurrentUser;
import com.sure.classsystemserver.security.home.dao.HomeDao;
import com.sure.classsystemserver.security.home.HomeService;
import com.sure.classsystemserver.security.home.MenuVO;
import com.sure.classsystemserver.security.home.Module;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HomeServiceImpl implements HomeService {

    @Resource
    private HomeDao homeDao;

    @Override
    public List<MenuVO> getMenus(CurrentUser currentUser) {

        List<Module> list =homeDao.findModuleByUser(currentUser.getUserId());

        List<MenuVO> menuVOList =new ArrayList<>();

        MenuVO menu =null;
        for (Module module :list){
            if (menu==null||!menu.getId().equals(module.getP_id())){
                menu=new MenuVO();
                menu.setId(module.getP_id());
                menu.setName(module.getP_name());
                menu.setChildren(new ArrayList<MenuVO>());

                menuVOList.add(menu);
            }

            MenuVO child =new MenuVO();
            child.setId(module.getM_id());
            child.setName(module.getM_name());
            child.setUrl(module.getM_web());

            menu.getChildren().add(child);
        }

        return menuVOList;
    }
}
